/**
 * 神州信息(2012)
 */
package net.abbp.report.view.cell.editor.eform;

import java.awt.Component;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import net.abbp.engine.ui.model.component.Text;
import net.abbp.report.model.Cell;
import net.abbp.report.view.cell.editor.AbstractCellEditor;
import net.abbp.report.view.grid.Grid;
import net.abbp.swing.layout.LayoutUtils;

import com.reportgear.core.swing.pane.base.EditorPane;
import com.reportgear.core.swing.pane.base.JBaseDialog;

/**
 * 文本框编辑器
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        <pre>
 * 历史：
 *      建立: Aug 1, 2012 lexloo
 * </pre>
 */
public class TextEditor extends AbstractCellEditor {
    private Text text;

    @Override
    public Component getCellEditorComponent(Grid grid, Cell cell) {
        TextEditPane editorPane = new TextEditPane();

        editorPane.populate((Text) cell.getValue());

        if (editorPane.showDialog(grid) == JBaseDialog.RESULT_OK) {
            this.text = editorPane.update();
        }

        return null;
    }

    @Override
    public Object getCellEditorValue() {
        return this.text;
    }

    private static class TextEditPane extends EditorPane<Text> {
        private Text value;

        public TextEditPane() {
            this.setLayout(new GridBagLayout());

            this.add(new JLabel("标签："), LayoutUtils.newGridBagConstraints(0, 0));
        }

        @Override
        public void populate(Text value) {
            this.value = value;
        }

        @Override
        public Text update() {
            return this.value;
        }

        @Override
        public int showDialog(Component owner) {
            return this.showDialog(SwingUtilities.getWindowAncestor(owner), "文本框", 300, 120);
        }
    }

}
